﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Support;
using MS.InformationManagement.BLL.SearchSender;
using MS.InformationManagement.DAL.DocumentCenter;
using Microsoft.SharePoint;
using System.Web.UI;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.Presenter.SearchSender
{
    public class SearchSenderPresenter : BasePresenter<ISearchSenderView>
    {
        #region Constructors

        public SearchSenderPresenter(ISearchSenderView view) : base(view) { }

        #endregion

        #region Methods

        public void HandleLoad()
        {
            View.AllowedRibbonActions = MS.InformationManagement.BLL.AllowingAccessControl.AllowingAccessControl.GetRibbonAllowedActionsCurrentUser();
            View.Show();
        }

        public void LoadSenders()
        {
            int totalRecords = 0;
            string camlQuery = getCamlQuery();
            
            if (View.Tipo == "Ciudadano")
                View.Personas = new PersonSearchSender().GetPerson(camlQuery, View.PageIndex, View.PageSize, out totalRecords);
            else
                View.Entidades = new EntitySearchSender().GetEntidades(camlQuery, View.PageIndex, View.PageSize, out totalRecords);

            View.TotalRecords = totalRecords;
            View.ShowSenders();
        }

        #endregion

        #region Private Methods

        private string getCamlQuery()
        {

            List<string> fields = new List<string>();
            List<string> values = new List<string>();
            List<string> types = new List<string>();
            List<string> operatorTypes = new List<string>();
            string contentTypeName = View.Tipo == "Ciudadano" ? GlobalConstants.CTTN_PEOPLE : GlobalConstants.CTTN_ENTITY;
            string contactName = View.Tipo == "Ciudadano" ? CustomSiteColumns.FLD_CONTACT_NAMES : CustomSiteColumns.FLD_CONTACT_BUSINESS_NAME;

            fields.Add("ContentType");
            values.Add(contentTypeName);
            types.Add("Computed");
            operatorTypes.Add("Eq");

            if (View.NIT_Cedula.Length > 0 && !View.NIT_Cedula.Trim().Equals(string.Empty))
            {
                fields.Add(CustomSiteColumns.FLD_CONTACT_ID);
                values.Add(View.NIT_Cedula);
                types.Add("Text");
                operatorTypes.Add("BeginsWith");
            }

            if (View.Nombres.Length > 0 && !View.Nombres.Trim().Equals(string.Empty))
            {
                fields.Add(contactName);
                values.Add(View.Nombres.Trim());
                types.Add("Text");
                operatorTypes.Add("Contains");
            }

            if (View.Apellidos.Length > 0 && !View.Apellidos.Trim().Equals(string.Empty))
            {
                fields.Add(CustomSiteColumns.FLD_CONTACT_FIRST_NAME);
                values.Add(View.Apellidos.Trim());
                types.Add("Text");
                operatorTypes.Add("Contains");
            }

            string _cadena = string.Empty;
            return CamlHelper.CreateCAMLQuery(values.ToArray(), fields.ToArray(), types.ToArray(), operatorTypes.ToArray(), ref _cadena);
        }

        #endregion
    }
}
